﻿using UnityEngine;
using UnityEngine.UI;
using UnityEditor;
using Jinndev;
using System.Collections.Generic;

namespace JinndevEditor {

    public static class CoreGameObjectMenu {

        [MenuItem(CommonEditorConst.GameObjectMenu + "/智能清除RaycastTarget", false)]
        public static void ClearRaycastTarget() {
            GameObject obj = Selection.activeGameObject;
            if (obj == null) {
                return;
            }
            Graphic[] graphics = obj.GetComponentsInChildren<Graphic>();
            foreach (var com in graphics) {
                if (com.gameObject.GetComponent<Button>() == null && com.gameObject.GetComponent<ScrollRect>() == null && com.gameObject.GetComponent<MouseClick>() == null) {
                    com.raycastTarget = false;
                }
            }
        }

        [MenuItem(CommonEditorConst.GameObjectMenu + "/全部AnchoredPosition.z重置为0", false)]
        public static void ResetAnchoredPositionZ() {
            GameObject obj = Selection.activeGameObject;
            if (obj == null) {
                return;
            }
            RectTransform[] rectTransforms = obj.GetComponentsInChildren<RectTransform>();
            foreach (var rectTransform in rectTransforms) {
                var pos = rectTransform.anchoredPosition3D;
                pos.z = 0;
                rectTransform.anchoredPosition3D = pos;
            }
        }

        [MenuItem(CommonEditorConst.GameObjectMenu + "/选中同级别同名对象", false)]
        public static void Init() {
            Transform transform = Selection.activeTransform;
            if (transform == null) {
                return;
            }
            List<GameObject> objs = new List<GameObject>();
            objs.Add(transform.gameObject);

            Transform parent = transform.parent;
            string path = transform.name;

            if (parent != null) {
                FindParent(parent, path, objs);
            }

            Selection.objects = objs.ToArray();
            Debug.Log("选中" + objs.Count + "个");
        }

        private static void FindParent(Transform transform, string path, List<GameObject> objs) {
            Transform parent = transform.parent;
            if (parent == null) {
                return;
            }
            for (int i = 0; i < parent.childCount; i++) {
                Transform child = parent.GetChild(i);
                if (child == transform) {
                    continue;
                }
                Transform target = child.Find(path);
                if (target != null) {
                    objs.Add(target.gameObject);
                }
            }

            path = parent.name + "/" + path;
            FindParent(parent, path, objs);
        }

        [MenuItem(CommonEditorConst.GameObjectMenu + "/复制路径", false)]
        public static void CopyPath() {
            GameObject obj = Selection.activeGameObject;
            GameObject rootObj = PrefabUtility.GetOutermostPrefabInstanceRoot(obj);
            Transform root = rootObj == null ? null : rootObj.transform;

            List<string> list = new List<string>();
            Transform transform = obj.transform;
            do {
                list.Add(transform.name);
                transform = transform.parent;
            }
            while (transform.parent != null && transform.parent != root);

            list.Reverse();
            string path = string.Join("/", list);
            GUIUtility.systemCopyBuffer = path;
        }

        [MenuItem(CommonEditorConst.GameObjectMenu + "/复制路径", true)]
        public static bool CopyPath_Validate() {
            return Selection.activeGameObject != null;
        }

    }

}
